from django.contrib.auth.models import Group
from rest_framework import serializers
from users.models import User


class PermissionAdminSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = '__all__'

    def create(self, validated_data):
        validated_data['is_superuser'] = True
        validated_data['is_staff'] = True
        user = super().create(validated_data)
        user.set_password(validated_data['password'])
        user.save()
        return user

    def update(self, instance, validated_data):
        instance = super().create(instance, validated_data)
        instance.set_password(validated_data['password'])
        instance.save()
        return instance


class PermissionGroupSimpleSerializer(serializers.ModelSerializer):
    class Meta:
        model = Group
        fields = '__all__'